#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include "testhelpers.c"

// tests buyCard

int main(){
    struct gameState *state = malloc(sizeof(struct gameState));

    printf("Testing BuyCard\n");

    resetFlag();
    setState(state);
    resetDeck(state);
    state->coins = 4;
    // tests buy cards when the state should be good
    if(buyCard(smithy, state)!= 0){
        printf("Error in buycard.  Buy should have succeeded.\n");
        incState();
    }
    if (state->coins!=0)
        assertMessage("Coins should be %d, it is %d\n", 0, state->coins);
    if (state->discardCount[0]!=1)
        assertMessage("Discard Count should be %d, it is %d\n", 1, state->discardCount[0]);
    if (state->discard[0][0] != smithy){
        printf("Error in buycard.  Smithy is not in the discard pile\n");
        incState();
    }
    if (state->supplyCount[smithy] != 9)
        assertMessage("Supply should be %d, it is %d\n", 9, state->supplyCount[smithy]);

    free(state);
    state = malloc(sizeof(struct gameState));
    setState(state);
    resetDeck(state);

    printf("Current discard count: %d\n", state->discardCount[0]);

    state->coins = 4;
    // bad state with no buys left
    state->numBuys = 0;
    if(buyCard(smithy, state) == 0){
        printf("Error in buycard.  No buys, Should not have succeeded\n");
        incState();
    }
    if (state->coins!=4)
        assertMessage("Coins should be %d, it is %d\n", 4, state->coins);
    if (state->discardCount[0]!=0)
        assertMessage("Discard Count should be %d, it is %d(no buys)\n", 0, state->discardCount[0]);
    if (state->discard[0][0] == smithy){
        printf("Error in buycard.  Smithy should not be in the discard pile\n");
        incState();
    }
    if (state->supplyCount[smithy] != 10)
        assertMessage("Supply should be %d, it is %d\n", 10, state->supplyCount[smithy]);

    free(state);
    state = malloc(sizeof(struct gameState));
    setState(state);
    resetDeck(state);

    printf("Current discard count: %d\n", state->discardCount[0]);

    state->coins = 4;
    // bad state with no supply left
    state->supplyCount[smithy] = 0;
    if(buyCard(smithy, state) == 0){
        printf("Error in buycard.  No buys, Should not have succeeded\n");
        incState();
    }
    if (state->coins!=4)
        assertMessage("Coins should be %d, it is %d\n", 4, state->coins);
    if (state->discardCount[0]!=0)
        assertMessage("Discard Count should be %d, it is %d(no supply)\n", 0, state->discardCount[0]);
    if (state->discard[0][0] == smithy){
        printf("Error in buycard.  Smithy should not be in the discard pile\n");
        incState();
    }
    if (state->supplyCount[smithy] != 0)
        assertMessage("Supply should be %d, it is %d\n", 0, state->supplyCount[smithy]);

    free(state);
    state = malloc(sizeof(struct gameState));
    setState(state);
    resetDeck(state);

    printf("Current discard count: %d\n", state->discardCount[0]);

    // bad state without enough coins
    state->coins = 2;
    if(buyCard(smithy, state) == 0){
        printf("Error in buycard.  No buys, Should not have succeeded\n");
        incState();
    }
    if (state->coins !=2)
        assertMessage("Coins should be %d, it is %d\n", 2, state->coins);
    if (state->discardCount[0]!=0)
        assertMessage("Discard Count should be %d, it is %d(not enough coins)\n", 0, state->discardCount[0]);
    if (state->discard[0][0] == smithy){
        printf("Error in buycard.  Smithy should not be in the discard pile\n");
        incState();
    }
    if (state->supplyCount[smithy] != 10)
        assertMessage("Supply should be %d, it is %d\n", 10, state->supplyCount[smithy]);

    if(testState() == 0)
        printf("Buycard Test Passed\n");
    else
        printf("Buycard Test Failed\n");

    free(state);
    return 0;
}
